﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;

namespace CoolERP_Web.User
{
    public partial class MenuEdit : BasePage
    {
        private Guid Mid
        {
            get { return this.GetUrlParam("id") != null ? new Guid(this.GetUrlParam("id").ToString()) : Guid.Empty; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindData();
        }

        private void BindData()
        {
            using (MenusBLL mBLL = new MenusBLL())
            {
                ddlParent.Items.Clear();
                ddlParent.DataSource = mBLL.GetList(t => t.ParentId == null).OrderBy(t => t.Code);
                ddlParent.DataTextField = "Name";
                ddlParent.DataValueField = "ID";
                ddlParent.DataBind();
                ddlParent.Items.Insert(0, new ListItem("一级菜单", ""));
                if (Mid != Guid.Empty)
                {
                    Menus entity = mBLL.GetEntity(t => t.Id == Mid);
                    if (entity.ParentId == null)
                        ddlParent.SelectedValue = "";
                    else
                        ddlParent.SelectedValue = entity.ParentId.ToString();
                    txtCode.Text = entity.Code;
                    txtName.Text = entity.Name;
                    txtUrl.Text = entity.Url;
                    txtSort.Text = entity.Sort == null ? "" : entity.Sort.ToString();
                    rblEnable.SelectedValue = entity.Enabled == null ? "0" : entity.Enabled.Value.ToString();
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int result = 0;
            using (MenusBLL mBLL = new MenusBLL())
            {
                Menus entity = new Menus();
                if (Mid != Guid.Empty)
                    entity = mBLL.GetEntity(t => t.Id == Mid);
                else
                    entity.Id = Guid.NewGuid();
                if (string.IsNullOrEmpty(ddlParent.SelectedValue))
                {
                    entity.ParentId = null;
                    entity.Path = entity.Id.ToString();
                }
                else
                {
                    entity.ParentId = new Guid(ddlParent.SelectedValue);
                    entity.Path = ddlParent.SelectedValue + "," + entity.Id.ToString();
                }
                entity.Code = txtCode.Text.Trim();
                entity.Name = txtName.Text.Trim();
                entity.Url = txtUrl.Text.Trim();
                entity.Enabled = Convert.ToInt32(rblEnable.SelectedValue);
                if (!string.IsNullOrEmpty(txtSort.Text.Trim()))
                    entity.Sort = Convert.ToInt32(txtSort.Text.Trim());
                else
                    entity.Sort = 0;
                result = mBLL.SaveEntity(entity, t => t.Id != entity.Id && t.ParentId == entity.ParentId && (t.Name == entity.Name || t.Code == entity.Code));
            }
            if (result == 1)
                labResult.Text = "保存成功";
            else if (result == -1)
                labResult.Text = "已存在此菜单，请重新输入";
            else
                labResult.Text = "保存失败";
        }
    }
}